package org.csu.mypetstore_seller.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore_seller.domain.Order;
import org.csu.mypetstore_seller.domain.OrderStatus;
import org.csu.mypetstore_seller.persistence.OrderMapper;
import org.csu.mypetstore_seller.persistence.OrderStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderStatusService {
    @Autowired
    OrderStatusMapper orderStatusMapper;
    @Autowired
    OrderMapper orderMapper;

    //主键查询单个订单
    public OrderStatus getOrderStatusById(int id){
        //return orderStatusMapper.selectById(id);
        if (id <= 0) {
            System.out.println("订单ID无效: {}"+id);
            return null; // 或者抛出 IllegalArgumentException
        }

        OrderStatus orderStatus = orderStatusMapper.selectById(id);
        if (orderStatus == null) {
            System.out.println("未找到订单状态，订单ID: {}"+id);
        }

        return orderStatus;
    }

    public List<OrderStatus> getOrderListStatusById(int id){//包装成List形式
        System.out.println(id);
        OrderStatus orderStatus=getOrderStatusById(id);
        List<OrderStatus> list=new ArrayList<>();
        list.add(orderStatus);
        System.out.println(list.get(0));
        return list;
    }
    public List<OrderStatus> getAllOrderListStatus(){
        return orderStatusMapper.selectList(null);
    }

    //由时间订单状态查询
    public List<OrderStatus> getOrderStatusListByTimestamp(Date timestamp){
        QueryWrapper<OrderStatus> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("timestamp",timestamp);
        return orderStatusMapper.selectList(queryWrapper);
    }
    public List<OrderStatus> getOrderStatusListByShipCity(String city){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("shipcity",city);
        List<Order> list =orderMapper.selectList(queryWrapper);
        List<OrderStatus> orderStatuses=new ArrayList<>();
        for(Order order:list){
            orderStatuses.add(orderStatusMapper.selectById(order.getOrderId()));
        }
        return orderStatuses;
    }
    public void deleteOrderStatusById(int id){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid", id);
        updateWrapper.set("delete_flag", -1);
        orderMapper.update(null, updateWrapper);
        orderStatusMapper.deleteById(id);
    }

    //存在逻辑问题需要修改，并且数据库没有订单状态字段(添加P)
    public void sendOutOrderById(int id){
        QueryWrapper<OrderStatus> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("orderid",id);
        OrderStatus orderStatus=orderStatusMapper.selectOne(queryWrapper);
       //添加一个已发货的提醒
        if("U".equals(orderStatus.getStatus())){
            throw new RuntimeException("该订单已经发货");
        }
        //orderStatus.setStatus("U");//U代表已经发货
        UpdateWrapper<OrderStatus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",id);
        updateWrapper.set("status","U");

        orderStatusMapper.update(null,updateWrapper);
    }


    public List<OrderStatus> getOrderStatus(String method,String str) throws ParseException {
        if(method.equals("id"))
            return getOrderListStatusById(Integer.parseInt(str));
            else if(method.equals("shipcity"))
                return getOrderStatusListByShipCity(str);
                else{
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date=simpleDateFormat.parse(str);
                    return getOrderStatusListByTimestamp(date);
                 }
    }

    //获取需要处理的订单数
    public Integer getNum(){
        QueryWrapper<OrderStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "P");//P代表未发货
        return orderStatusMapper.selectList(queryWrapper).size();
    }
}
